fix(watch): dispatch unload and process exit events on restart#32664
Merged
bartlomieju merged 1 commit intodenoland:mainfrom Mar 13, 2026
Merged
fix(watch): dispatch unload and process exit events on restart#32664bartlomieju merged 1 commit intodenoland:mainfrom
bartlomieju merged 1 commit intodenoland:mainfrom
Conversation
Previously, `pending_unload` was only set after both module execution
and the load event, so cancellation during a top-level await meant the
unload event never fired. Now `pending_unload` is set before module
execution and reset on error, ensuring the unload event fires even when
the future is cancelled mid-evaluation.
Also adds `dispatch_process_exit_event()` to the Drop impl so that
Node.js `process.on("exit")` handlers fire on watch restart, matching
the normal shutdown path.
Co-Authored-By: Claude Opus 4.6 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
unloadevent not firing when watch mode restarts a script blocked on a top-level awaitprocess.on("exit")handlers not firing on watch restartWhat changed
In
FileWatcherModuleExecutor::execute(),pending_unloadwas previously setafter
execute_main_module()anddispatch_load_event(). This meant that ifthe operation future was cancelled during a top-level await (the module never
finished evaluating),
pending_unloadstayedfalseand theDropimpl neverdispatched the
unloadevent.Now
pending_unloadis set beforeexecute_main_module()and reset tofalseonly on module evaluation error (syntax errors, import failures). Whenthe future is cancelled (dropped),
pending_unloadremainstrueandDropcorrectly dispatches both the
unloadevent and theprocess exitevent.The
Dropimpl also now callsdispatch_process_exit_event()alongsidedispatch_unload_event(), so Node.jsprocess.on("exit")handlers fire onrestart — matching the normal shutdown path.
Towards #30912
Test plan
run_watch_unload_on_restartintegration test — verifiesaddEventListener("unload", ...)fires on file change restartrun_watch_process_exit_on_restartintegration test — verifiesprocess.on("exit", ...)fires on file change restart🤖 Generated with Claude Code